<?php

/**
 * 模拟人类
 * 
 * @param string name
 * @author whq
 * @todo 什么时候调用使用用品不明确
 *
 */
class Person 
{
	private $_sex;
	private $_isChangedSex = false;
	private $_name;
	private $_hand;
	private $_supply = array(
	    'wood' => null,
	    'gains' => null,
	);
	/**
	 * 人类使用的工具数组
	 * @var iTool 
	 */
	private $_tool = array();
	
	/**
	 * 体温
	 * @var float default 37.5
	 */
	private $_bodyTemperature = 36.8;
	
	/**
	 * 饱腹度
	 * @var int
	 */
	private $_hungry = 0;
	const MAN = "男";
	const WOMAN = "女";
	
	public function __construct( $name = 'Unknown Name' )
	{
		if( !$name || $name == 'Unknown Name' )
		{
			return false;
		}
		$this->_name = $name;
	}
	
	/**
	 * @return the $_isChangedSex
	 */
	public function getIsChangedSex() 
	{
		return $this->_isChangedSex;
	}

	/**
	 * 
	 * 设置性别
	 * @param field_type $_sex
	 */
	public function setSex( $_sex )
	{
		if( $_sex != Person::MAN || $_sex != Person::WOMAN )
		{
			return false;
		}
		//判断是否为变性
		if( !empty($this->_sex) && $this->_sex != $_sex  )
		{
			$this->_isChangedSex = true;
		}
		$this->_sex = $_sex;
	}
	
	/**
	 * 砍树
	 * @param Tree $tree
	 */
	public function cutTree( $key )
	{
	    if ( !$this->_supply['wood'] )
	    {
	        $this->_supply['wood'] = new Wood( $this );
	    }
		if ( !array_key_exists( 'chopper', $this->_tool ) )
		{
			$this->_tool['chopper'] = new Chopper();
		}
		if ( $this->_tool['chopper']->useTool( $key ) )
		{
		    $this->_supply['wood']->addSupply( 1 );
		}
	}
	
	/**
	 * 摘果
	 * @param Tree $tree
	 */
	public function getGains( $key )
	{
	    if ( !$this->_supply['gains'] )
	    {
	        $this->_supply['gains'] = new Gains( $this );
	    }
		if ( !array_key_exists( 'clipper', $this->_tool ) )
		{
			$this->_tool['clipper'] = new Clipper();
		};
		$bool = $this->_tool['clipper']->useTool( $key );
		//var_dump($bool);
		if ( $bool )
		{
		    $this->_supply['gains']->addSupply( 1 );
		}
		return $bool;
	}
	
	/**
     * @return the $_bodyTemperature
     */
    public function getBodyTemperature ()
    {
        return $this->_bodyTemperature;
    }

	/**
     * @return the $_hungry
     */
    public function getHungry ()
    {
        return $this->_hungry;
    }

	/**
     * @param int $_hungry
     */
    public function setHungry ($_hungry)
    {
        $this->_hungry = $_hungry;
    }

	/**
     * @param float $_bodyTemperature
     */
    public function setBodyTemperature ($_bodyTemperature)
    {
        $this->_bodyTemperature = $_bodyTemperature;
    }

	/**
	 * 查找森林那颗树可以砍伐
	 * @return  Tree array
	 */
	public function whichCanBeCut( $forest = null )
	{
		$forest = $forest == null ? Forest::getInstance():$forest;
		$treesCanBeCut = array();
		foreach ( $forest as $key => $tree )
		{
			if ( ( $tree->getwizened() || $tree->getDied() ) && $tree->getCanBeCut() > 0 )
			{
				$treesCanBeCut[] = $key;
			}
		}
		return $treesCanBeCut;
	}
	
	/**
	 * 查找森林那颗树可以摘果
	 * @return  Tree array
	 */
	public function whichCanGetGains( $forest = null )
	{
		$forest = $forest == null ? Forest::getInstance():$forest;
		$treesCanGetGains = array();
		foreach ( $forest as $key => $tree )
		{
			if ( $tree->getGainsValue() > 0 )
			{
				$treesCanGetGains[] = $key;
			}
		}
		return $treesCanGetGains;
	}
	
	/**
	 * 使用用品
	 * @param string $supply 用品类别['wood','gains']
	 * @return boolean
	 */
	public function useSupply( $supply )
	{
	    if ( $this->_supply[ $supply ] )
	    {
	        $this->_supply[ $supply ]->useSupply();
	        return true;
	    }
	    else return false;
	}
	
	public function checkSupply( $type )
	{
	   // echo "fasd". $type;
	  // var_dump( $this->_supply );
	    if ( $this->_supply[ $type ] )
	    {
	        return $this->_supply[ $type ]->checkNum();
	    }
	    return 0;
	}
	public function getInformation()
	{
		echo '<br />'. $this->_name ."的个人信息：";
		echo "<br />体温：". $this->getBodyTemperature();
		echo "<br />饱腹度：". $this->getHungry();
		echo "<br />性别：". $this->_sex;
		echo "<br />拥有木材：". $this->checkSupply( 'wood' );
		echo "<br />拥有果实：". $this->checkSupply( 'gains' );
	}
}

?>